package com.suitsoft.ut.service.base;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jxl.Sheet;
import jxl.Workbook;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.dao.account.UserDao;
import com.suitsoft.ut.dao.base.CollegeDao;
import com.suitsoft.ut.dao.base.TeacherTempDao;
import com.suitsoft.ut.entity.account.Role;
import com.suitsoft.ut.entity.account.User;
import com.suitsoft.ut.entity.base.College;
import com.suitsoft.ut.entity.base.Teacher;
import com.suitsoft.ut.entity.base.TeacherTemp;
import com.suitsoft.ut.service.ServiceException;
import com.suitsoft.ut.service.account.AccountManager;
import com.suitsoft.ut.util.ExcelDataError;
import com.suitsoft.ut.util.ExcelTools;
import com.suitsoft.ut.util.StringUtils;

/**
 * 学生临时信息管理 
 *@author Suine
 */
@Service
@Transactional
public class TeacherTempManager {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	private final int COLS = 8;

	@Autowired
	private TeacherTempDao dao;
	@Autowired
	private AccountManager accountManager;
	@Autowired
	private CollegeDao collegeDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private TeacherManger teacherManger;
	
	
	public void deleteAll(){
		dao.deleteAll();
	}

	public TeacherTemp get(Long id){
		return dao.get(id);
	}
	
	public List<TeacherTemp> get(List<Long> ids){
		return dao.get(ids);
	}
	
	public List<TeacherTemp> getAll(){
		return dao.getAll();
	}
	
	public void save(TeacherTemp temp){
		dao.save(temp);
	}
	
	/**
	 * 读取Excel信息
	 * @param file
	 * @param errors
	 * @return
	 */
	public List<ExcelDataTeacher> handleData(File file,List<ExcelDataError> errors){
		// 打开Excel工作簿
		Workbook workbook = ExcelTools.openExcelWorkbook(file.getPath());
		Sheet sheet = workbook.getSheet(0);
		int rowCount = sheet.getRows();
		int colCount = sheet.getColumns();
		
		if (COLS != colCount) {
			errors.add(new ExcelDataError(1, "-", "Excel文件格式无效！"));
			throw new ServiceException("文件格式无效");
		}
		
		List<ExcelDataTeacher> excelDatas = Lists.newArrayList();
		
		// 循环处理所有行, 将数据读入到内存中
		for (int i = 1; i < rowCount; i++) {
			try {
				ExcelDataTeacher edl = new ExcelDataTeacher(sheet, i);
				excelDatas.add(edl);
			} catch (ServiceException e) {
				logger.warn("文件中有空行，实际读到行数：" + i);
				errors.add(new ExcelDataError(i, "-", "文件中有空行，实际读到行数：" + i + 1));
				throw new ServiceException("文件中有空行，实际读到行数：" + i);
			}
			
		}
		// 关闭Excel工作簿
		ExcelTools.closeExcelWorkbook(workbook);
		checkData(excelDatas, errors);
		return excelDatas;
	}
	
	/**
	 * 检查数据合法性
	 * @param datas
	 * @param errors
	 */
	private void checkData(List<ExcelDataTeacher> datas,List<ExcelDataError> errors){
		Set<ExcelDataTeacher> setData = new HashSet<ExcelDataTeacher>();
		int i = 1;
		for(ExcelDataTeacher d : datas){
			i ++;
			if(StringUtils.isEmpty(d.getCollege())){
				errors.add(new ExcelDataError(i + 1,"学院","为空"));
			}
			if(StringUtils.isEmpty(d.getName())){
				errors.add(new ExcelDataError(i + 1,"教师姓名","为空"));
			}
			if(StringUtils.isEmpty(d.getNo())){
				errors.add(new ExcelDataError(i + 1,"工号","为空"));
			}
//			if(StringUtils.isEmpty(d.getTelephone())){
//				errors.add(new ExcelDataError(i + 1,"联系电话","为空"));
//			}
//			if(StringUtils.isEmpty(d.getTitile())){
//				errors.add(new ExcelDataError(i + 1,"职称","为空"));
//			}
//			if(StringUtils.isEmpty(d.getWorkDate())){
//				errors.add(new ExcelDataError(i + 1,"工作时间","为空"));
//			}else if(!DateUtils.isLegalDate(d.getWorkDate())){
//				errors.add(new ExcelDataError(i + 1,"工作时间","非法日期"));
//			}
			
			if(setData.contains(d)){
				int inx = datas.indexOf(d);
				errors.add(new ExcelDataError(i,"-","与第" + (inx + 2) + "行的工号重复"));
			}else{
				setData.add(d);
			}
		}
	}
	
	
	public List<TeacherTemp> findBy(String name,String value){
		return dao.findBy(name, value);
	}
	
	/**
	 * 保存数据到正式数据中.即保存到BD_表中.
	 * @param teachers
	 */
	public void saveData(List<TeacherTemp> teachers) throws ServiceException{
		//可登陆角色
		Role ableLoginRole = getRoleByName(Constants.CN_ABLE_LOGIN_USER);
		//教师角色
		Role teacherRole = getRoleByName(Constants.CN_TEACHER);;
		//时间角色
		Role timeContrlRole = getRoleByName(Constants.CN_TIME_ROLE);
		//权限 集
		List<Role> roles = Lists.newArrayList();
		roles.add(ableLoginRole);
		roles.add(teacherRole);
		roles.add(timeContrlRole);
		
		Teacher teacher = null;
		User user = null;
		College college = null;
		for(TeacherTemp st: teachers){
			college = collegeDao.isHaveCollgeNoNew(st.getCollege());
			teacher = new Teacher();
			teacher.copy(st);
			teacher.setCollege(college);
			
			
			user = new User();
			user.setCollegeCode(college.getId()+"");
			user.setEmail(st.getEmail());
			user.setLoginName(st.getNo());
			user.setName(st.getName());
			user.setPassword(Constants.DEFALT_PASSWORD);
			
			user.setRoleList(roles);
			accountManager.saveUser(user);
			
			teacher.setForUser(user);
			
			userDao.save(user);
			
			teacherManger.save(teacher);
			
			//设置为已确认
			st.setIsConfirm(1L);
			dao.save(st);
		}
	}
	
	
	/**
	 * 获取权限集合
	 * @param name
	 * @return
	 */
	public Role getRoleByName(String name){
		List<Role> roles = accountManager.findRoleByGroupName(name);
		if(roles == null || roles.isEmpty()){
			throw new ServiceException("系统内置角色丢失：" +name);
		}else if(roles.size() != 1){
			throw new ServiceException("系统内置角色存在重复：" + name);
		}
		return roles.get(0);
	}
	
	public List<Map<String,Object>> getImpList(){
		return  dao.getImpList();
	}
}

